﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace VBACodeingTool
{
    public abstract class LoaderProjectCode
    {
        ScopeType defaultSubScopeType = ScopeType._Public;
        ScopeType defaultVariableScopeType = ScopeType._Private;
        ParentType parentType;
        string codeContent;
        string parentName;
        public List<ProjectCodeData> projectCodeData = new List<ProjectCodeData>();

        public LoaderProjectCode(string codeContent, string parentName, ParentType parentType)
        {
            CodeContent = codeContent;
            ParentName = parentName;
            ParentType = parentType;
        }

        public ScopeType DefaultSubScopeType { get => defaultSubScopeType; set => defaultSubScopeType = value; }
        public ScopeType DefaultVariableScopeType { get => defaultVariableScopeType; set => defaultVariableScopeType = value; }
        public string CodeContent { get => codeContent; set => codeContent = value; }
        public string ParentName { get => parentName; set => parentName = value; }
        public ParentType ParentType { get => parentType; set => parentType = value; }

        public abstract List<ProjectCodeData> ParseCode();
        protected void ParseSubs()
        {
            MatchCollection publicSubs = RegexManager.publicSubRegex.Matches(CodeContent);
            string content = string.Empty;
            foreach ( Match sub in publicSubs)
            {
                if (sub.Success)
                {
                    content = sub.Groups[1].Value;
                    projectCodeData.Add(new ProjectCodeData(false, content, content,40,
                        ProjectCodeType.Sub, DefaultSubScopeType, ParentName, ParentType));
                }
            }
            MatchCollection privateSubs = RegexManager.privateSubRegex.Matches(CodeContent);
            foreach (Match sub in privateSubs)
            {
                if (sub.Success)
                {
                    content = sub.Groups[1].Value;
                    projectCodeData.Add(new ProjectCodeData(false, content, content, 40,
                        ProjectCodeType.Sub, ScopeType._Private, ParentName, ParentType));
                }
            }
        }
        protected void ParseVariable()
        {
            string afterReplace = RegexManager.replaceSubRegex.Replace(CodeContent, "");
            MatchCollection privateVariables = RegexManager.privateVariableRegex.Matches(afterReplace);
            string content = string.Empty;
            foreach (Match variable in privateVariables)
            {
                if (variable.Success)
                {
                    content = variable.Groups[1].Value;
                    MatchCollection variables = RegexManager.variableRegex.Matches(content);
                    foreach (Match variable1 in variables)
                    {
                        if (variable1.Success)
                        {
                            content = variable1.Groups[1].Value.Trim();
                            projectCodeData.Add(new ProjectCodeData(false, content, content, 40,
                        ProjectCodeType.Variable, DefaultVariableScopeType, ParentName, ParentType));
                        }
                    }

                }
            }
            MatchCollection publicVariables = RegexManager.publicVariableRegex.Matches(afterReplace);
            foreach (Match variable in publicVariables)
            {
                if (variable.Success)
                {
                    content = variable.Groups[1].Value;
                    MatchCollection variables = RegexManager.variableRegex.Matches(content);
                    foreach (Match variable1 in variables)
                    {
                        if (variable1.Success)
                        {
                            content = variable1.Groups[1].Value.Trim();
                            projectCodeData.Add(new ProjectCodeData(false, content, content, 40,
                        ProjectCodeType.Variable, ScopeType._Public, ParentName, ParentType));

                        }
                    }

                }
            }
        }
    }
}
